import Vue from 'vue'
import Router from 'vue-router'
import Login from "../components/Login";
import Home from "../components/Home";
import Welcome from "../components/Welcome";
import User from "../components/User"
import Rights from "../components/power/Rights";
import Roles from "../components/power/Roles";
import Cate from "../components/goods/Cate"
import Params from "../components/goods/Params";
import List from "../components/goods/List";
import Add from "../components/goods/Add";
import Order from "../components/order/Order";
import Report from "../components/report/index"

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/login', name: 'login', component: Login},
    { path: '/', redirect: "/login"},
    {path: '/home',
      component: Home,
      // 重定向路由 访问/home 的时候会重定向到/welcome路由
      redirect: '/welcome',
      // 配置二级路由
      children: [
        {path: '/welcome', component: Welcome},
        {path: '/users', component: User},
        {path: '/rights', component: Rights},
        {path: '/roles', component: Roles},
        {path: '/categories', component: Cate},
        {path: '/params', component: Params},
        {path: '/goods', component: List},
        {path: '/goods/add', component: Add},
        {path: '/orders', component: Order},
        {path: '/reports', component: Report},
      ]},
  ]
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = window.sessionStorage.getItem('token');
    if (!token) {
      // 注意： 这里不能this.$router.push跳转，因为这样会无限的router.beforeEach操作
      return next('/login')
    } else {
      next()
    }

  }

})

export default router
